package org.example.batchutils.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.example.batchutils.mapper.DataSourceMapper;
import org.example.batchutils.pojo.Result;
import org.example.batchutils.service.ExcelImportDatasource;
import org.example.batchutils.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

@Slf4j
@Service
public class ExcelImportDatasourceImpl implements ExcelImportDatasource {

    @Autowired
    private ExcelUtils excelUtils;

    @Autowired
    private DataSourceMapper dataSourceMapper;

    @DS("#param.poolName")
    @Override
    public Result importData(MultipartFile file, String poolName) {
        try {
            log.info("当前数据源: " +  DynamicDataSourceContextHolder.peek());
            excelUtils.importExcelToDatabase(file, 5);
            return Result.success();
        }
        catch (Exception e)
            {
                return Result.error(e.getMessage());
        }
    }


    @Override
    public List<String> getAllPollName() {
        return dataSourceMapper.getAllPollName();
    }


}
